<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Util\Err;
use App\Http\Util\HomeTool;
use App\Http\Util\Upload;
use App\Models\Menus;
use App\Models\Roles;
use Illuminate\Http\Request;

class RolesController extends Controller
{
    /**获取列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request)
    {
        $page = request()->only(['page','limit']);
        $list = Roles::where(function ($query) use ($page) {
            if (array_key_exists('keyword',$page) && $page['keyword']) {
                $query->where('name', 'like', '%' . $page['keyword'] . '%');
//                $query->orWhere('content', 'like', '%'.$page['keyword'].'%');
            }
            $query->where('is_del',  Err::DEL_SHOW);
        })->orderBy('id', 'desc')
            ->offset(($page['page'] - 1) * $page['limit'])
            ->limit($page['limit'])
            ->get(['id','name','created_at','menus','updated_at'])->toArray();
        $count = Roles::where(function ($query) use ($page) {
            if (array_key_exists('keyword',$page) && $page['keyword']) {
                $query->where('name', 'like', '%' . $page['keyword'] . '%');
//                $query->orWhere('content', 'like', '%'.$page['keyword'].'%');
            }
            $query->where('is_del',  Err::DEL_SHOW);
        })->count();
        return response()->json(['data' => $list, 'total' => $count, 'code' => 200]);
    }

    /**获取全部select
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function all(Request $request)
    {
        $list = Roles::where('is_del',  Err::DEL_SHOW)->orderBy('id', 'desc')
            ->get(['id','name'])->toArray();
        return response()->json(['data' => $list, 'code' => 200]);
    }


    /**
     * 详情
     */
    public function detail(){
        try{
            $id = request()->get('id');
            $model= Roles::where('id',$id)->first(['menus','name'])->toArray();
            $menus =[];
            if($model['menus']){
                $model['menus'] = json_decode($model['menus']);
                $menus = Menus::whereIn('id',$model['menus'])->get(['id','label'])->toArray();
            }
            return response()->json(['code'=>200,'msg'=>'ok','menus'=>$menus]);
        }catch(\Exception $e){
            echo $e->getMessage();
        }
    }

    /**创建身份
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function create(Request $request)
    {
        $data = $request->post();
        $save['name']=$data['name'];
        $arr =[];
        if(count($data['menus'])>0){
            foreach($data['menus'] as $v){
               array_push( $arr,$v['id']);
            }
            $save['menus'] = json_encode($arr,true);
        }
        if (array_key_exists('id',$data) && $data['id'] > 0) {
            $check_roles = HomeTool::checkRoles(48,'edit');
            if(!$check_roles){
                return response()->json(['code' => 1001, 'msg' => '暂无此操作权限']);
            }
            $id = $data['id'];
            unset($data['id']);
            if(array_key_exists('updated_at', $data)){
                unset($data['updated_at']);
            }
            Roles::where('id', $id)->update($save);
        } else {
            $check_roles = HomeTool::checkRoles(48,'add');
            if(!$check_roles){
                return response()->json(['code' => 1001, 'msg' => '暂无此操作权限']);
            }
            Roles::create($save);
        }
        return response()->json(['code' => 200, 'msg' => '成功']);
    }

    /**权限细分详情
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function roles_detail(Request $request){
        $id = $request->get('id');
        $roles = Roles::where('id',$id)->first()->toArray();
        if($roles['menus']){
            $menus = json_decode($roles['menus'],true);
        }
        if($roles['roles']){
            $roles['roles'] = json_decode($roles['roles'],true);
        }else{
            $roles['roles'] = [];
        }
        $roles['menus_name'] = Menus::whereIn('id',$menus)->get()->where('component','<>','#')->toArray();
        foreach( $roles['menus_name'] as $k => $v){
            if(!array_key_exists($v['id'],$roles['roles'])){
                $roles['roles'][$v['id']] =[];
            }
        }
        return response()->json(['code' => 200, 'data' => $roles]);
    }



    /**保存权限细分
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function save_roles(Request $request){
        $check_roles = HomeTool::checkRoles(48,'edit');
        if(!$check_roles){
            return response()->json(['code' => 1001, 'msg' => '暂无此操作权限']);
        }
        $post = $request->post();
        $post['roles'] = $post['roles']?json_encode($post['roles']):[];
        $models = Roles::where('id',$post['id'])->update(['roles'=>$post['roles']]);
        if($models){
            return response()->json(['code' => 200, 'msg' => '成功']);
        }
        return response()->json(['code'=>1001,'msg'=>'请稍后重试']);

    }

    /**删除角色
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function del(Request $request)
    {
        $check_roles = HomeTool::checkRoles(48,'del');
        if(!$check_roles){
            return response()->json(['code' => 1001, 'msg' => '暂无此操作权限']);
        }
        $id = $request->get('id');
        if (!$id) {
            return response()->json(['code' => 1001, 'msg' => '无此条信息']);
        }
        $model = Roles::find($id);
        $model->is_del = Err::DEL_HIDE;
        $model->save();
        return response()->json(['code' => 200, 'msg' => '删除成功']);
    }
}
